//||=========================================================================||
//||                                                                         ||
//||                            MandelbrotWgt.cpp                            ||
//||                                                                         ||
//||                           by the_best_goose))                           ||
//||                                                                         ||
//||=========================================================================||

#include <assert.h>
#include <QMouseEvent>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <stdlib.h>
#include "MandelbrotWgt.h"

//-----------------------------------------------------------------------------

MandelbrotWgt::MandelbrotWgt(QWidget* pmand_pic, const MainWindow* pwind,
		                     QWidget* pwgt):
QWidget(pwgt),
main_layout_(),
params_layout_(),
params_(),
ppic_(pmand_pic),
t_lbl_(),
mapping_lbl_(),
iters_count_(),
coord_lbl_(),
t_re_(0.0f),
t_im_(0.0f)
{
	assert(pwind);
	assert(pmand_pic);

	connect(pwind, SIGNAL(IterationsCountChanged(int)),
			this, SLOT(SetIterationsCount(int)));
	connect(pwind, SIGNAL(MappingChanged(string)),
			this, SLOT(SetMapping(string)));
	connect(pwind, SIGNAL(TReChanged(float, bool)), this, SLOT(SetTRe(float)));
	connect(pwind, SIGNAL(TImChanged(float, bool)), this, SLOT(SetTIm(float)));
	connect(pmand_pic, SIGNAL(CoordChanged(QPointF)),
			this, SLOT(SetCoord(QPointF)));

	params_layout_.addWidget(&coord_lbl_);
	params_layout_.addWidget(&t_lbl_);
	params_layout_.addWidget(&iters_count_);
	params_layout_.addWidget(&mapping_lbl_);
	params_.setLayout(&params_layout_);
	main_layout_.addWidget(ppic_);
	main_layout_.addWidget(&params_);
	setLayout(&main_layout_);
}

//-----------------------------------------------------------------------------

MandelbrotWgt::~MandelbrotWgt()
{
	assert(ppic_);
	delete ppic_;
}

//-----------------------------------------------------------------------------

void MandelbrotWgt::SetMapping(string mapping)
{
	mapping_lbl_.setText((string("Z -> ") + mapping).c_str());
}

//-----------------------------------------------------------------------------

void MandelbrotWgt::SetIterationsCount(int count)
{
	char buf[100] = {0};
	sprintf(buf, "iterations count: %d", count);
	iters_count_.setText(buf);
}

//-----------------------------------------------------------------------------

void MandelbrotWgt::SetTRe(float re)
{
	t_re_ = re;
	char buf[100] = {0};
	if(t_im_ >= 0)
        sprintf(buf, "T = %.5f + %.5fi", t_re_, t_im_);
	else
        sprintf(buf, "T = %.5f - %.5fi", t_re_, fabs(t_im_));
	t_lbl_.setText(buf);
}

//-----------------------------------------------------------------------------

void MandelbrotWgt::SetTIm(float im)
{
	t_im_ = im;
	char buf[100] = {0};
	if(t_im_ >= 0)
        sprintf(buf, "T = %.5f + %.5fi", t_re_, t_im_);
	else
        sprintf(buf, "T = %.5f - %.5fi", t_re_, fabs(t_im_));
	t_lbl_.setText(buf);
}

//-----------------------------------------------------------------------------

void MandelbrotWgt::SetCoord(QPointF coord)
{
	char buf[100] = {0};
	if(coord.y() >= 0)
        sprintf(buf, "C = %.5f + %.5fi", coord.x(), coord.y());
	else
        sprintf(buf, "C = %.5f - %.5fi", coord.x(), fabs(coord.y()));
	coord_lbl_.setText(buf);
}
